package com.ltsznh.android.frequencycounter;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;

/**
 * Created by 涛 on 13-12-24.
 */
public class FrequencyThread extends Thread {
    private final SurfaceHolder compassSH;
    private Paint cPaint = new Paint();
    private Canvas canvas;

    private int cWidth = 0;
    private int cHeight = 0;
    private float cX = 0;
    private float cY = 0;
    private float cR = 0;
    private double zR = 0.85;

    public FrequencyThread(SurfaceHolder surfaceHolder) {
        // TODO Auto-generated constructor stub
        compassSH = surfaceHolder;

        cPaint.setColor(Color.rgb(0, 221, 255));
        cPaint.setAntiAlias(true);//锯齿效果
    }

    protected void setSize(int x,int y){
        cWidth = x;
        cHeight = y;

        cX = cWidth/2;
        cY = cHeight/2;
        cR = Math.min(cX,cY) * 9/10;
    }

    public void run(){
        canvas = compassSH.lockCanvas();

        try {
            synchronized (compassSH) {

                drawCompass();//画表盘

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                compassSH.unlockCanvasAndPost(canvas);
            }
        }
    }

    public void drawCompass(){

        cPaint.setStyle(Paint.Style.STROKE);//设置空心
        cPaint.setStrokeWidth(6);//线条粗细
        canvas.drawCircle(cX,cY,cR,cPaint);
        cPaint.setStyle(Paint.Style.FILL);//设置实心
        canvas.drawCircle(cX,cY,10,cPaint);//画中心
//                画指针
        cPaint.setStrokeWidth(5);
        canvas.drawLine(cX,(float)(cY + cR * zR),cX,(float)(cY - cR * zR),cPaint);
        canvas.drawLine((float)(cX - cR * zR),cY,(float)(cX + cR * zR),cY,cPaint);

//                画刻度
        cPaint.setStrokeWidth(6);
        cPaint.setTextSize(18);
    }
}
